package com.hgvip.init;

import com.hgvip.job.InvokeJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Created by arnold.zhu on 2017/8/2.
 */
@Component
public class ScheduleRunner implements CommandLineRunner {

    @Override
    public void run(String... strings) throws Exception {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();

        //获取 任务作业
        JobDetail jobDetail = newJob(InvokeJob.class).withIdentity("groupJobName", "group1").build();

        CronScheduleBuilder cronBuilder = CronScheduleBuilder.cronSchedule("0/7 * * * * ?");

        //获取 触发器
        Trigger trigger = newTrigger()
                .withIdentity("triggerName", "group1")
                .withSchedule(cronBuilder)
                .startNow().build();

        //绑定
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }
}
